<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/admins.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<style>
    .layui-upload-file{
        display: block !important;
        position: absolute;
        left: 52%;
        top: 0;
        width: 103px;
        height: 38px;
        z-index: 99;
    }
</style>
<body>
<div class="layuimini-container">

    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <span class="layui-card-header"><i class="layui-icon layui-icon-form"></i>&nbsp;流程部署列表</span>
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn floatR" lay-event="draw"> 在线绘制流程 </button>
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn floatR" lay-event="add"> 上传流程文件 </button>
            </div>
        </script>

        <table class="layui-hide" id="ProcessDeploymentsTableId" lay-filter="currentTableFilter"></table>
        <!--分页-->
        <div class="layui-table-page layui-border-box">
            <div id="barcon" class="page_div" name="barcon"></div>
        </div>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-start" lay-event="start">启动实例</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-look" lay-event="look">查看</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除部署</a>
        </script>

    </div>
</div>
<!-- 表单添加bpmn弹窗 -->
<script type="text/html" id="ProcessDepEditDialog">
    <form id="ProcessDepForm" lay-filter="ProcessDepForm" class="layui-form model-form" enctype="multipart/form-data">
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">上传文件:</label>
            <div class="layui-input-block">
                <input  id="fileName" placeholder="" class="layui-input DialogInput"
                       lay-verType="tips"  disabled/>
                <button type="button" class="layui-btn DialogBtn" id="uploadBpmn"><i class="layui-icon"></i>上传BPMN</button>
                <input class="layui-upload-file" type="file" accept="" name="processFile" id="uploadFile">
            </div>
        </div>
<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label layui-form-required">流程名称:</label>-->
<!--            <div class="layui-input-block">-->
<!--                <input name="deploymentName" id="deploymentName" placeholder="请输入流程名称" class="layui-input"-->
<!--                       lay-verType="tips" lay-verify="required" required/>-->
<!--            </div>-->
<!--        </div>-->
        <div class="layui-form-item text-center">
            <button class="layui-btn" lay-filter="ProcessDepEditSubmit" lay-submit id="submit">提交</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">返回</button>
        </div>
    </form>
</script>
<!--启动实例弹窗-->
<script type="text/html" id="stratProcessDepEditDialog">
    <form id="strarProcessDepForm" lay-filter="strarProcessDepForm" class="layui-form model-form">
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">实例名称:</label>
            <div class="layui-input-block">
                <input name="deploymentName" id="instanceName" placeholder="请输入实例名称" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">实例描述:</label>
            <div class="layui-input-block">
                <input name="deploymentName" id="instanceVariable" placeholder="请输入实例描述" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item text-center">
            <button class="layui-btn" lay-filter="startProcessDepEditSubmit" lay-submit id="submit2">提交</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">返回</button>
        </div>
    </form>
</script>
<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script src="../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../js/ManualPaging.js"></script>
<script>
        layui.use(['form', 'table','admin','upload','laypage'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            upload = layui.upload,
            laypage = layui.laypage,
            admin = layui.admin;
        /*渲染列表*/
        var limitcount = 10;
        var curnum = 1;
        var insTb = table.render({
            elem: '#ProcessDeploymentsTableId',
            toolbar: '#toolbarDemo',
            defaultToolbar: [],
            url: ''+publicurl+'processDefinition/getDefinitions',
            parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": res.status === 0 ? 0 : res.status, //解析接口状态,默认成功是0
                    "msg": res.msg, //解析提示文本
                    "count": res.obj.length, //解析数据长度
                    "data": res.obj //解析数据列表
                };
            },
            cols: [[
                {type: "checkbox", width:'5%',align:"center"},
                {field: 'DeploymentId', width:'27%', title: '编号', sort: true,align:"center"},
                {field: 'Name', width:'18%', title: '流程名称',align:"center"},
                {field: 'ResourceName',width:'20%', title: '流程文件名称',align:"center"},
                {field: 'Version', width:'10%', title: '部署版本',align:"center"},
                {title: '操作', width:'20%', toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: false,
            skin: 'line',
            // id:'ProcessDeploymentsTableId',
            done:function (res, curr, count) {
                if(count>0){
                    goPage(1);
                }
            }
        });
        /**
         * toolbar添加bpmn事件
         * 新增  ?type=addBpmn
            编辑  ?type=editBpmn&id=xxx
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                showEditModel();
            }else if(obj.event === 'draw'){
                window.open(''+publicurl+'bpmnjs/dist/index.html?type=addBpmn');
            }
        });
        /*表格启动 删除事件*/
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            var editData = JSON.stringify(data);
            if (obj.event === 'start') {
                stratProcessDepEditDialog(obj);
            } else if (obj.event === 'delete') {
                doDel(obj);
            }else if(obj.event === 'look'){
                window.open(''+publicurl+'bpmnjs/dist/index.html?type=lookBpmn&deploymentFileUUID='+data.DeploymentId+'&deploymentName='+data.ResourceName+'');
            }
        });
        /* 上传流程文件 */
        function showEditModel(mData) {
            admin.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '信息',
                content: $('#ProcessDepEditDialog').html(),
                success: function (layero, dIndex) {
                    //上传BPMN-输入框赋值
                    var $fileName =$("#fileName");
                    $fileName.val("");
                    $("#uploadFile").on("change",function (e) {
                        var e = e || window.event;
                        var files = e.target.files;
                        if(files.length>0){
                            // 获取文件名 并显示文件名
                            var fileName = files[0].name;
                            $fileName.val(fileName);
                        }else{
                            //清空文件名
                            $fileName.val("");
                        }
                    });
                    // 表单提交事件
                    form.on('submit(ProcessDepEditSubmit)', function (data) {
                        layer.close(dIndex);
                        var fileObj = document.getElementById("uploadFile").files[0];
                        if (typeof (fileObj) == "undefined" || fileObj.size <= 0) {
                            layer.msg('请选择文件');
                            return;
                        };
                        var formFile = new FormData();
                        formFile.append('processFile',fileObj);
                        // formFile.append('deploymentName',$('input[name="deploymentName"]').val());
                        // var data = formFile;
                        var loadIndex = layer.load(2);
                        $.ajax({
                            cache : true,
                            type : "post",
                            url :  ''+publicurl+'processDefinition/uploadStreamAndDeployment',
                            async : false,
                            data : formFile,
                            contentType: false,
                            processData: false,
                            success : function(res) {
                                layer.close(loadIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload();
                            },
                            error : function(res) {
                                layer.close(dIndex);
                                layer.alert(res.msg+'<br>'+res.obj, {
                                    icon: 2,
                                    title:"提示"
                                });
                            }
                        });
                        return false;
                    });
                    // 禁止弹窗出现滚动条
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }
        /* 删除部署 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.get( ''+publicurl+'processDefinition/delDefinition', {
                    pdID: obj.data ? obj.data.DeploymentId : '',
                }, function (res) {
                    layer.close(loadIndex);
                    if (res.status === 0) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                        console.log(obj,"删除数据");
                        insTb.reload();
                    } else {
                        layer.alert(res.msg+'<br>'+res.obj, {icon: 2});
                    }
                }, 'json');
            });
        };
        /* 启动实例*/
        function stratProcessDepEditDialog(obj) {
            admin.open({
                type: 1,
                title:  '启动流程部署信息',
                content: $('#stratProcessDepEditDialog').html(),
                success:function(layero, dIndex){
                    form.on('submit(strarProcessDepForm)', function (data) {
                        var loadIndex = layer.load(2);
                        $.get( ''+publicurl+'processInstance/startProcess',
                            {
                                processDefinitionKey:obj.data.Key,
                                instanceName:$("#instanceName").val(),
                                instanceVariable:$("#instanceVariable").val()
                        }, function (res) {
                            layer.close(loadIndex);
                            if (res.status === 0) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload();
                            } else {
                                layer.close(dIndex);
                                layer.alert(res.msg+'<br>'+res.obj, {icon: 2});
                            }
                        }, 'json');
                        layer.close(loadIndex);
                        return false;
                    })
                }
            })
        }



    });
</script>

</body>
</html>